<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Controllers;

use NRG\Framework\Common\Registry as Registry;
use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Database\Mappers as Mappers;

/**
 * QA viewer
 *
 * @package NRG\Framework\Controllers
 */
class Qa extends BaseController
{
    /**
     * XNAT object
     * 
     * @var Xnat 
     */
    protected $_xnat;

    /**
     * Pre-dispatch of action
     */
    public function preDispatch()
    {
        $this->_xnat = Registry::get("xnat");
    }

    /**
     * Index action
     */
    public function indexAction()
    {
        $this->adminOnly();

        $start = (int) trim($this->_session->getRequestParam("start", false));
        
        if($start > 0)
            $offset = $start * 7;
        else
            $offset = 0;

        $count_select = "SELECT count(*) FROM neuroinfo_extendedboldqc,img_assessor_out_resource,xnat_resource
                         WHERE neuroinfo_extendedboldqc.id=xnat_imageassessordata_id
                         AND xnat_resource.xnat_abstractresource_id=img_assessor_out_resource.xnat_abstractresource_xnat_abstractresource_id
                         AND format='image/png'
                         AND scan_n_vols=124";
        
        $select = "SELECT * FROM neuroinfo_extendedboldqc,img_assessor_out_resource,xnat_resource
                   WHERE neuroinfo_extendedboldqc.id=xnat_imageassessordata_id
                   AND xnat_resource.xnat_abstractresource_id=img_assessor_out_resource.xnat_abstractresource_xnat_abstractresource_id
                   AND format='image/png'
                   AND scan_n_vols=124
                   ORDER BY scan_qc_ssnr DESC,id,content LIMIT 70 OFFSET $offset";
        
        $count_result = $this->_xnat->query($count_select);
        $select_result = $this->_xnat->query($select);

        $keys = array('id',
                      'scan_qc_mean',
                      'scan_qc_stdev', 
                      'scan_qc_ssnr', 
                      'scan_qc_vsnr', 
                      'scan_mot_rel_xyz_mean',
                      'scan_mot_rel_xyz_max',
                      'scan_mot_abs_xyz_mean',
                      'scan_mot_abs_xyz_max',
                      'scan_mot_rel_xyz_1mm',
                      'scan_mot_rel_xyz_5mm');

        $label = null;
        $result = array();
       
        foreach($select_result as $row)
        {
            
            if($label != $row['id'])
            {
                $label = $row['id'];
                $result[$label] = array('metrics' => array(), 'images' => array());
                   
                foreach($keys as $key)
                    $result[$label]['metrics'][$key] = $row[$key];
            }

            $filename = basename($row['uri']);
            $file_id = $row['xnat_abstractresource_id'];
            $exp_id = $row['scan_session_id'];
            $ass_id = $row['id'];
            $uri = "/data/experiments/$exp_id/assessors/$ass_id/out/resources/$file_id/files/$filename";
            $result[$label]['images'][$row['content']] = $uri;
        }
        
        $this->_view->result = $result;
        $this->_view->start = $start;
    }
} 
